
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404, _get_queryset
from com.ericyang.common.web import setting
from com.ericyang.common.web.middleware import threadlocals

# get model object
def getobj(klass, *args, **kwargs):
    return _get_queryset(klass).get(*args, **kwargs)

#redirect the target url
def redirect(url, *args):
    return HttpResponseRedirect(url, *args)

# reader template to response
def response(template, args={}):
    user = threadlocals.get_current_user()
    args.update({'user':user})
    return render_to_response(template, args)

# response without user in args
def clear_response(template, args={}):
    return render_to_response(template, args)

#redirect the target url
def url_redirect(url, *args):
    def inner(request, *args):
        return HttpResponseRedirect(url)
    return inner

# response the template for the configuration in the url 
def url_response(template, *args):
    def inner(request, *args):
        return render_to_response(template, *args)
    return inner

# redirect to 404 page
def page404():
    return response(setting.PAGE_404,{})

def error_response(message):
    return response("error.html", {"message":message})

#list error
def list_error(obj):
    result = ""
    if obj.errors:
        result += "<ol>"
        for error in obj.errors:
            result += "<li><strong>" + error | escape + "</strong></li>"
        endfor
        result += "</ol>"
    return result;